<cfcomponent displayname="CrafterGroupedListCollection" extends="crafter.CrafterListCollection" output="no" accessors="yes">


	<!--- Properties --->
	<cfproperty name="collectionNameQuickReference" type="struct" />
	
	
	<!--- init() --->
	<cffunction name="init" access="public" returntype="any" output="no">
		<cfargument name="crafterFactory" type="crafter.CrafterFactory" required="yes" />
		<cfargument name="collectionName" type="string" required="no" default="test" />
		<cfscript>
			super.init(
				crafterFactory = arguments.crafterFactory,
				collectionName = arguments.collectionName
			);
			setCollectionNameQuickReference(StructNew());
			return this;
		</cfscript>
	</cffunction>
	
	
	<!--- load() and loadByPath() --->
	<!--- NOTE: This object does not have an explicit load() or loadByPath() methods because it is extending
	crafter.CrafterListCollection and those methods would be identical. --->
	
	
	<!--- loadByDocument() --->
	<cffunction name="loadByDocument" access="public" returntype="any" hint="" output="no">
		<cfargument name="doc" type="crafter.CrafterDocument" required="true" />
		
		<cfscript>
			// Pull out the item groups
			local.arGroups = XmlSearch(arguments.doc.getXml(),'//itemGroup');
			
			// Check that item groups were found
			if (NOT ArrayIsEmpty(local.arGroups)) {
				
				// Item groups found. Loop over them.
				for (var i=1; i LTE ArrayLen(local.arGroups); i++) {
					
					// Create a CrafterListCollection object to represent this group
					local.oGroupCollection = CreateObject("component","crafter.CrafterListCollection").init(crafterFactory = getCrafterFactory());
					local.oGroupCollection.setCollectionName(local.arGroups[i].XmlChildren[1].XmlText);
					
					// Loop over the items in the group and add them to the CrafterListCollection
					// as simple text paths to the item.  The CrafterListCollection.getItem() method will
					// take care of the conversion process when necssary.
					for (var x=2; x LTE ArrayLen(local.arGroups[i].XmlChildren); x++) {
						local.oGroupCollection.addToCollection(local.arGroups[i].XmlChildren[x].XmlText);
					};
					
					// Now that the CrafterListCollection has been populated, add it to the 
					// master collection along with a quickRefence.
					addToCollection(item=local.oGroupCollection);
					addQuickReference(
						name = local.oGroupCollection.getCollectionName(),
						index = i
					);
				}
			}
		</cfscript>
	</cffunction>
	
	
	<!--- addQuickReference() --->
	<cffunction name="addQuickReference" access="private" returntype="any" output="no">
		<cfargument name="name" type="string" required="yes" />
		<cfargument name="index" type="numeric" required="yes" />
		<cfset StructInsert(getCollectionNameQuickReference(), arguments.name, arguments.index, "YES") />
	</cffunction>
	
	
	<!--- getCollectionByGroupName() --->
	<cffunction name="getCollectionByGroupName" access="public" returntype="any" output="no">
		<cfargument name="name" type="string" required="yes" />
		<cfscript>
			// Use the collectionNameQuickReference property to search for and retrieve the
			// collection index that is associated with this name and return the item at that index.
			local.index = StructFind(getCollectionNameQuickReference(), arguments.name);
			return getCollection()[local.index];
		</cfscript>
	</cffunction>
	

</cfcomponent>